

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>角色管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-role" data-type="add">添加</button>
            </div>

            <table id="table" lay-filter="table"></table>
            <script type="text/html" id="table-useradmin-admin">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                <!--<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>-->
            </script>
        </div>
    </div>
</div>
<!--<script type="text/html" id="typeTpl">-->
        <!--<div class="layui-input-block" style="margin:0 auto">-->
            <!--<input value="{{d.rol_id}}" type="checkbox" {{#  if(d.rol_type ==1){ }} checked {{#  } else { }} {{#  } }} name="open" lay-skin="switch" lay-filter="switchTest" lay-text="启用|禁用" >-->
        <!--</div>-->
<!--</script>-->
<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table'], function(){
        var $ = layui.$
            ,form = layui.form
            ,table = layui.table;

        //创建表格
        table.render({
            elem: '#table'
            ,url:"{:url('admin/role/roleList')}"
            ,cols: [[
                {field: 'rol_id', width: 80, title: 'ID', sort: true,align: 'center'}
                ,{field: 'rol_name', title: '角色名',align: 'center'}
                ,{field: 'rol_remark', title: '权限描述',align: 'center'}
                // ,{field: 'rol_remark', title: '权限描述',align: 'center' ,toolbar:"#typeTpl"}
                ,{title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-useradmin-admin'}
            ]]
            ,text: '对不起，加载出现异常！'
        });

        //监听工具条
        table.on('tool(table)', function(obj){
            //data={rol_id: 8, rol_name: "44", rol_list: "admin|company", rol_remark: "44", rol_type: 0}
            var data = obj.data;
            //{rol_id: 1, rol_name: "超级管理员", rol_list: "", rol_remark: "", rol_type: 1}
            if(obj.event === 'del'){
                layer.confirm('确定删除此角色？', function(index){
                    //从table中删除该信息
                    $.ajax({
                        url: "{:url ('admin/admin/delRole')}?rol_id="+data.rol_id,
                        type: "post",
                        dataType: "json",
                        success: function (res) {
                            if (res.code == 1) {
                                layer.msg(res.msg, {icon: 1, time: 1500}, function () {
                                    obj.del();
                                });
                            } else {
                                layer.msg(res.msg, {icon: 5, time: 1500}, function () {
                                });
                            }
                        }
                    })

                    layer.close(index);
                });
            }
            else if(obj.event === 'edit'){
                var tr = $(obj.tr);
                layer.open({
                    type: 2
                    ,title: '编辑角色'
                    ,content: "{:url ('admin/role/roleEdit')}?rol_id="+obj.data.rol_id
                    ,area: ['600px', '480px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                            ,submit = layero.find('iframe').contents().find("#LAY-user-role-submit");

                        //监听提交
                        iframeWindow.layui.form.on('submit(LAY-user-role-submit)', function(data){
                            var field = data.field; //获取提交的字段

                            $.ajax({
                                url: "{:url ('admin/role/roleEdit')}?rol_id="+obj.data.rol_id,
                                type: "post",
                                data: field,
                                dataType: "json",
                                success: function (res) {
                                    if (res.code == 1) {
                                        layer.msg('编辑成功', {icon: 1, time: 1500}, function () {
                                            // parent.location.reload(); //刷新父亲对象（用于框架）
                                        });
                                    } else {
                                        layer.msg(res.msg, {icon: 5, time: 1500}, function () {
                                        });
                                    }
                                }
                            })
                            //提交 Ajax 成功后，静态更新表格中的数据
                            //$.ajax({});
                            table.reload('table'); //数据刷新
                            layer.close(index); //关闭弹层
                        });

                        submit.trigger('click');
                    }
                    ,success: function(layero, index){

                    }
                })
            }
        });

        //监听switch 开关，用来直接在列表中修改状态，而不是在编辑里面修改
        // form.on('switch(switchTest)', function(data){
        //     //获取当前switch 的数据 ：
        //     // 1. 在value上绑定数据id = data.value
        //     // 2. 获取这个开关的状态 = data.elem.checked
        //     var value=data.value;
        //     var checked=data.elem.checked?1:0;
        //
        //     $.ajax({
        //         url: "{:url ('admin/admin/changeType')}",
        //         type: "post",
        //         dataType: "json",
        //         data:{
        //             "rol_id":value,
        //             "rol_type":checked
        //         },
        //         success: function (res) {
        //             layer.msg(res.msg, {time: 1500}, function () {
        //             });
        //         }
        //     })
        //
        // });

        //事件
        var active = {
            //批量删除操作
            // batchdel: function(){
            //     var checkStatus = table.checkStatus('table')
            //         ,checkData = checkStatus.data; //得到选中的数据
            //
            //     //checkData：{{rol_id: 6, rol_name: "测试角色3", rol_list: "", rol_remark: "123", rol_type: 0},
            //     //            {rol_id: 7, rol_name: "123", rol_list: "", rol_remark: "123", rol_type: 0}}
            //     if(checkData.length === 0){
            //         return layer.msg('请选择数据');
            //     }
            //
            //     //将要删除的id，组装为逗号分割的字符串
            //     var ids="";
            //     for(var i=0;i<checkData.length;i++){
            //         ids+=checkData[0]['rol_id']+",";
            //     }
            //
            //     layer.confirm('确定删除吗？', function(index) {
            //
            //         //发送ajax请求，删除指定的数据
            //         $.ajax({
            //             url: "{:url ('admin/admin/delMultiRole')}",
            //             type: "post",
            //             data: {
            //                 "ids":id
            //             },
            //             dataType: "json",
            //             success: function (res) {
            //                 if (res.code == 1) {
            //                     layer.msg(res.msg, {icon: 1, time: 1500}, function () {
            //                     });
            //                 } else {
            //                     layer.msg(res.msg, {icon: 5, time: 1500}, function () {
            //                     });
            //                 }
            //             }
            //         })
            //         table.reload('table');
            //     });
            // },
            //添加新角色
            add: function(){
                layer.open({
                    type: 2
                    ,title: '添加新角色'
                    ,content:"{:url ('admin/role/roleAdd')}"
                    ,area: ['600px', '480px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                            ,submit = layero.find('iframe').contents().find("#LAY-user-role-add-submit");

                        //监听提交
                        iframeWindow.layui.form.on('submit(LAY-user-role-add-submit)', function(data){
                            var field = data.field; //获取提交的字段
                            $.ajax({
                                url: "{:url ('admin/role/roleAdd')}",
                                type: "post",
                                data: field,
                                dataType: "json",
                                success: function (res) {
                                    if (res.code == 1) {
                                        layer.msg('添加成功！', {icon: 1, time: 1500}, function () {
                                        });
                                    } else {
                                        layer.msg(res.msg, {icon: 5, time: 1500}, function () {
                                        });
                                    }
                                }
                            })
                            //重载表格
                            table.reload('table');
                            layer.close(index); //关闭弹层
                        });
                        submit.trigger('click');
                    }
                });
            }
        }
        $('.layui-btn.layuiadmin-btn-role').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>

